<template>
  <div class="login">
    <v-header :back-display="true">
      <div slot="title">登 录</div>
    </v-header>

    <div class="logo">
      <img src="@/assets/logo.png" alt="">
    </div>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="账号 :"
        clearable
        maxlength="8"
        left-icon="user-circle-o"
        placeholder="请输入邮箱/用户名/手机号"
        :rules="[{ required: true }]"
      />
      <van-field
        v-model="password_a"
        type="password"
        name="password_a"
        label="密码 :"
        clearable
        maxlength="6"
        autocomplete="off"
        left-icon="lock"
        placeholder="密码为6位数字和字母组合"
        :rules="[{ pattern, required: true }]"
      />
      <div class="login-btn">
        <van-button class="btn" native-type="submit">登 录</van-button>
        <van-button native-type="button" @click="tohome" class="btn Rbtn">参加考试</van-button>
      </div>
      <div class="bottom">
        <p>没有账号 ? 点击</p>
        <router-link class="router" to="/user/regist">注 册</router-link>
      </div>
    </van-form>
  </div>
</template>

<script>
import api from '../../api/api'
import { mapActions } from 'vuex'
import header from '@/components/main/header'
import { Form, Field,Button, Toast, Dialog} from 'vant'


export default {
  components: {
    "v-header":header,
    [Form.name]: Form,
    [Field.name]: Field,
    [Button.name]: Button,
    [Toast.name]: Toast,
    [Dialog.Component.name]: Dialog.Component,
  },
  data() {
    return{
      //  用户 student  密码  abc123  boss要求密码是 6位数字与字母组合
      username:'',
      password:'abc123',
      password_a:'',
      pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6}$/,  // 密码正则
    }
  },
  methods:{
    ...mapActions(['setUserInfo']),

    //提交表单  登录
    onSubmit(data) {
      // let that = this
      if( this.password_a !== this.password ){
        Dialog.alert({title: '登录失败',message: '账号或密码错误, 请重新输入',}).then(() => {
        });
      }else{
        console.log('提交登录表单', data);
        const toast1 = Toast.loading({message: '登录中...',forbidClick: true})
        api.Login(data).then(res=>{
          console.log("登录响应",res)
          if(res.data.success){
            toast1.clear()
            Toast.success('登录成功')
            this.setUserInfo(res.data)   //mapAction 传参到 actions里请求数据
            setTimeout(()=>{
              this.$router.push({path:'/home'})
            },2000)
          }
        }).catch(error=>{
          toast1.clear()
          Toast.fail('登录失败');
          console.log(error)
        })
      }
    },

    // 参加考试
    tohome(){
      this.$router.push({path:'/home'})
    }
  }
}
</script>

<style lang="less" scoped>
.logo{
  width: 180px;
  margin: 70px auto 30px;
  img{
    display: block;
    width: 100%;
  }
}
.van-form{ width: 80%; margin: 0 auto;}
/deep/.van-field__label {width: 50px;}
/deep/.van-field__left-icon {margin-right: 6px;}
.login-btn{
  display: flex;
  justify-content: space-around;
  margin-top: 50px;
  .btn{
    background-color: #ff6400;
    width: 40%;
    border-radius: 5px;
    color: #fff;
    text-align: center;
    line-height: 40px;
    height: 40px;
    font-size: 14px;
  }
  .Rbtn{
    background-color: #3D9DB3;
  }
}
.bottom{
  display: flex;
  justify-content: center;
  font-size: 16px;
  margin-top: 40px;
  p{color: #333;margin-right: 10px;}
  .router{color: #ff6400;}
}
</style>
